<?php

/**
 * This is the model class for table "tb_student".
 *
 * The followings are the available columns in table 'tb_student':
 * @property integer $id
 * @property string $full_name
 * @property string $nickname
 * @property string $birthday
 * @property string $birthplace
 * @property string $address
 * @property integer $child
 * @property integer $sibling
 * @property integer $religion_id
 * @property string $blood_type
 * @property string $sex
 * @property string $student_number
 * @property string $photo
 * @property integer $created_by
 * @property string $created
 * @property integer $modified_by
 * @property string $modified
 *
 * The followings are the available model relations:
 * @property RefReligion $religion
 * @property TenantStudent[] $tenantStudents
 */
class Student extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_student';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('full_name, nickname, birthday, birthplace, address, sex, student_number', 'required'),
			array('child, sibling, religion_id, created_by, modified_by', 'numerical', 'integerOnly'=>true),
			array('full_name, birthplace', 'length', 'max'=>100),
			array('nickname', 'length', 'max'=>50),
			array('address', 'length', 'max'=>300),
			array('blood_type', 'length', 'max'=>2),
			array('sex', 'length', 'max'=>6),
			array('student_number', 'length', 'max'=>20),
			array('photo', 'length', 'max'=>200),
			array('created, modified', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, full_name, nickname, birthday, birthplace, address, child, sibling, religion_id, blood_type, sex, student_number, photo, created_by, created, modified_by, modified', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'religion' => array(self::BELONGS_TO, 'RefReligion', 'religion_id'),
			'tenantStudents' => array(self::HAS_MANY, 'TenantStudent', 'student_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'full_name' => 'Full Name',
			'nickname' => 'Nickname',
			'birthday' => 'Birthday',
			'birthplace' => 'Birthplace',
			'address' => 'Address',
			'child' => 'Child',
			'sibling' => 'Sibling',
			'religion_id' => 'Religion',
			'blood_type' => 'Blood Type',
			'sex' => 'Sex',
			'student_number' => 'Student Number',
			'photo' => 'Photo',
			'created_by' => 'Created By',
			'created' => 'Created',
			'modified_by' => 'Modified By',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		
		$criteria->compare('id',$this->id);
		$criteria->compare('full_name',$this->full_name,true);
		$criteria->compare('nickname',$this->nickname,true);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('birthplace',$this->birthplace,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('child',$this->child);
		$criteria->compare('sibling',$this->sibling);
		$criteria->compare('religion_id',$this->religion_id);
		$criteria->compare('blood_type',$this->blood_type,true);
		$criteria->compare('sex',$this->sex,true);
		$criteria->compare('student_number',$this->student_number,true);
		$criteria->compare('photo',$this->photo,true);
		$criteria->compare('created_by',$this->created_by);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified_by',$this->modified_by);
		$criteria->compare('modified',$this->modified,true);


		if(Sms::isTenantUser()){
			$criteria->alias = 'a';
			$criteria->join = 'inner join tb_tenant_student b on b.student_id = a.id';
			$criteria->condition = 'b.tenant_id = '.Yii::app()->user->getState('tenant_id');
		}

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Student the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function afterSave(){
		if($this->isNewRecord){
			$newUser = new TenantStudent();
			$newUser->tenant_id = Yii::app()->user->getState('tenant_id');
			$newUser->student_id = $this->id;
			$newUser->save(); 
		}

		return parent::afterSave();
	}
}
